package com.medranocg.gd



import static org.springframework.http.HttpStatus.*
import com.medranocg.gd.alfresco.Utils
import grails.transaction.Transactional

@Transactional(readOnly = true)
class ConsultaDocumentosController {

	def documentService
	
    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

	def modalShow(Document documentInstance) {
		def listVersion = Document.findAllByCustomerAndDocumentTypeAndDeleteDate(
				documentInstance.customer, documentInstance.documentType, null).
				sort({it.documentVersion as Float}).reverse()
		
		respond(documentInstance, model: [listVersion: listVersion])
	}
	
	@Transactional
	def anular(Document documentInstance) {
		def result = documentService.anular(documentInstance, session.usuario.userName)
		
		def listVersion = Document.findAllByCustomerAndDocumentTypeAndDeleteDate(
			documentInstance.customer, documentInstance.documentType, null).
			sort({it.documentVersion as Float}).reverse()
		
		if (result) {
			render(view: 'modalShow', model: [documentInstance: result.document, listVersion: listVersion])
		}
		else {
			render(view: 'modalShow', model: [documentInstance: documentInstance, listVersion: listVersion])
		}
	}	

}
